package tripController;

import com.jfinal.core.Controller;
import com.jfinal.kit.HashKit;

import tripModel.User;
import tripService.TotalService;
import tripService.UserService;

public class UserController extends Controller {
	public void reg() {
		render("reg.jsp");
	}

	public void doregByEmail() {
		User user = getModel(User.class, "");
		String uName = user.getStr("uName");
		String uPassword = user.getStr("uPassWord");
		if (uName == null) {
			render("login.jsp");
			return;
		}
		boolean isok = user.set("uName", uName).set("uEmail", HashKit.md5(user.getStr("uEmail")))
				.set("uPassWord", HashKit.md5(uPassword)).save();
		if (isok) {
			setAttr("message", true);
		} else {
			setAttr("message", false);
		}
		render("message.jsp");
	}

	public void doregByPhone() {
		User user = getModel(User.class, "");
		String uName = user.getStr("uName");
		String uPassword = user.getStr("uPassWord");
		if (uName == null) {
			render("login.jsp");
			return;
		}
		boolean isok = user.set("uName", uName).set("uPhone", HashKit.md5(user.getStr("uPhone")))
				.set("uPassWord", HashKit.md5(uPassword)).save();
		if (isok) {
			setAttr("message", true);
		} else {
			setAttr("message", false);
		}
		render("message.jsp");
	}

	public void login() {
		render("login.jsp");
	}

	public void doLoginByEmail() {
		User user = getModel(User.class, "");
		String uEmail = user.getStr("uEmail");
		String uPassWord = user.getStr("uPassWord");
		if (uEmail == null || uPassWord == null) {
			render("login.jsp");
			return;
		}
		User u = UserService.loginByEmail(uEmail, uPassWord);
		if (u != null) {
			setAttr("totalList", TotalService.showTotal());// 主题
			getSession().setAttribute("user", u);
			render("user.jsp");
			return;
		}
		setAttr("mes", "账号或密码错误");
		render("login.jsp");
	}

	public void doLoginByPhone() {
		// User user = getModel(User.class, "");
		String uPhone = getPara("uPhone");
		String uPassWord = getPara("uPassWord");
		if (uPhone == null || uPassWord == null) {
			render("login.jsp");
			return;
		}
		User u = UserService.loginByPhone(uPhone, uPassWord);
		if (u != null) {
			setAttr("totalList", TotalService.showTotal());// 主题
			getSession().setAttribute("user", u);
			render("user.jsp");
			return;
		}
		setAttr("mes", "账号或密码错误");
		render("login.jsp");
	}
}
